/*
 * LoggerConfigurator.cpp
 *
 *  Created on: Dec 17, 2008
 *      Author: brt
 */

#include "../include/LoggerConfigurator.h"

#include "../include/Logger.h"

LoggerConfigurator::LoggerConfigurator(Properties& props)
  : m_Properties(props)
{
}
LoggerConfigurator::LoggerConfigurator()
{

}

const Properties&
LoggerConfigurator::Current() const
{
	return m_Properties;
}

void
LoggerConfigurator::Add(Properties& props)
{
	Properties::iterator it = props.begin();
	Properties::iterator end = props.end();

	for (; it != end; ++it)
	{
		m_Properties.setProperty(it->first, it->second);
	}
}


void
LoggerConfigurator::Clear()
{
	m_Properties.clear();
}

void
LoggerConfigurator::Configure()
{
	Configure(LogParameters::instance());
}

void
LoggerConfigurator::Configure(LogParameters& logParams)
{
	try {
		std::string log_on = m_Properties.propertyValue("LOG_ENABLED");

		if (log_on == "YES") logParams.active(true);
		else logParams.active(false);

		std::string log_lvl = m_Properties.propertyValue("LOG_LEVEL");

		if (log_lvl == "TRACE")
			logParams.level(log_TRACE);
		else if (log_lvl == "DEBUG")
			logParams.level(log_DEBUG);
		else if (log_lvl == "INFO")
			logParams.level(log_INFO);
		else if (log_lvl == "WARN")
			logParams.level(log_WARN);
		else if (log_lvl == "ERROR")
			logParams.level(log_ERROR);
		else {
			std::cout << "Invalid log level, defaults to WARN" << std::endl;
			logParams.level(log_WARN);
		}

		LOG(log_INFO) << "Log level set to " << log_lvl;

	} catch (std::exception& ex) {
		std::cout << "Error reading configuration file" << std::endl;
		throw;
	}
}

LoggerConfigurator::~LoggerConfigurator() {
}
